/*******************************************************************************
 * BanneddomainsDelegator.java
 *
 * Created by Nishan
 * Last modified Date: Sep 9, 2008 2:23:35 PM by nishan
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/

package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.*;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import com.printeverywhere.dto.Banneddomain;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class BanneddomainsDelegator.
 */
public class BanneddomainsDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(BanneddomainsDelegator.class);
	
	/**
	 * Adds the banned domain.
	 * 
	 * @param banneddomain the banneddomain
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void addBannedDomain(Banneddomain banneddomain) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called addBannedDomain method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(banneddomain);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert Banned Domain ".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	/**
	 * Gets the banned domain.
	 * 
	 * @return the banned domain
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<Banneddomain> getBannedDomain() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for blocked Domain and Email".intern());
		}
		Session session = HibernateUtil.getSession();
		try{
			session.beginTransaction();
			List<Banneddomain> banneddomainlist = session.createCriteria(Banneddomain.class).list();
			return banneddomainlist;
		}catch (HibernateException e) {
			log.error("Got exception when getting list for blocked Domain and Email".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	/**
	 * Gets the banned domainby id.
	 * 
	 * @param id the id
	 * 
	 * @return the banned domainby id
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static Banneddomain getBannedDomainbyId(int id) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for blocked Domain and Email by Id".intern());
		}
		Session session = HibernateUtil.getSession();
		try{
			session.beginTransaction();
			Criteria criteria = session.createCriteria(Banneddomain.class);
			criteria.add(Restrictions.eq(AttributeConstants.BANNED_DOMAIN_ID , id));
			List<Banneddomain> banneddomainlist = criteria.list();
			return ((banneddomainlist.size() > 0) ? banneddomainlist.get(0) : new Banneddomain());
		}catch (HibernateException e) {
			log.error("Got exception when getting list for blocked Domain and Email by Id".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	/**
	 * Update banned domain.
	 * 
	 * @param banneddomain the banneddomain
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateBannedDomain(Banneddomain banneddomain) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateBannedDomain method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.update(banneddomain);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Update Banned Domain ".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	/**
	 * Delete banned domain.
	 * 
	 * @param banneddomain the banneddomain
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteBannedDomain(Banneddomain banneddomain) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called deleteBannedDomain method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(banneddomain);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Delete Banned Domain ".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
}